/* 
 * Copyright (c) 2019, CENTRIN.CIYUN.LTD. All rights reserved.
 */
package com.cory.admin.sys.controller.portal;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONObject;
import com.cory.admin.sys.config.AdminConfig;
import com.cory.admin.sys.config.AdminConstants;
import com.cory.admin.sys.domain.AdminPrincipal;
import com.cory.admin.sys.domain.entity.SysResource;
import com.cory.admin.sys.domain.portal.MenuVO;
import com.cory.admin.sys.domain.portal.ServiceVO;
import com.cory.admin.sys.enums.EResourceType;
import com.cory.boot.base.BaseController;
import com.cory.boot.domain.bean.Result;
import com.cory.boot.domain.enumdef.EDataFlag;
import com.google.common.collect.Lists;

/**
 * 首页
 * @author xiongcong
 * @date 2019-02-26
 */
@Controller
@RequestMapping("portal/index")
public class IndexController extends BaseController {
    @Autowired
    private AdminConfig adminConfig;
    
    @GetMapping("")
    public String index(Model model) {
        if ("basics".equals(adminConfig.getModel()) && StringUtils.isNotBlank(adminConfig.getIndexCode())) {
            model.addAttribute("code", adminConfig.getIndexCode());
            return String.format("portal/%s/iframe", AdminConstants.MODEL);
        }
        return String.format("portal/%s/index", AdminConstants.MODEL);
    }
    
    @RequestMapping("getServiceList")
    @ResponseBody
    @SuppressWarnings("unchecked")
    public Result<?> hmo_getServiceList(HttpSession session) {
        List<SysResource> resources = (List<SysResource>)session.getAttribute(AdminConstants.SESSION_USER_RESOURCE);
        
        List<SysResource> services = resources.stream().filter(i -> i.getType() == EResourceType.SERVICE.key).collect(Collectors.toList());
        
        List<ServiceVO> list = Lists.newArrayList();
        services.forEach(service -> {
            ServiceVO vo = new ServiceVO(service.getId(), service.getName(), service.getServiceType(), service.getIcon());
            
            List<SysResource> folders = resources.stream().filter(folder -> folder.getPid().equals(service.getId())).collect(Collectors.toList());
            List<SysResource> menus = Lists.newArrayList();
            folders.forEach(folder -> {
                List<SysResource> folderMenus = resources.stream().filter(menu -> menu.getPid().equals(folder.getId())).collect(Collectors.toList());
                menus.addAll(folderMenus);
            });
            
            vo.setLinkMenuId(menus.size() > 0 ? menus.get(0).getId() : null);
            menus.sort((a, b) -> {
                Integer a_indexFlag = a.getIndexShowFlag() == null ? EDataFlag.NO.key : a.getIndexShowFlag();
                Integer b_indexFlag = b.getIndexShowFlag() == null ? EDataFlag.NO.key : b.getIndexShowFlag();
                return a_indexFlag.compareTo(b_indexFlag);
            });
            
            List<MenuVO> menuVOs = menus.stream().limit(3).map(i -> new MenuVO(i.getId(), i.getName(), i.getCode())).collect(Collectors.toList());
            vo.setMeuns(menuVOs);
            list.add(vo);
        });
        
        list.removeIf(i -> {
            return i.getMeuns() == null || i.getMeuns().size() == 0;
        });
        
        return Result.success("ok", list);
    }
    
    @RequestMapping("getUserInfo")
    @ResponseBody
    public Result<?> getUserInfo(HttpSession session) {
        AdminPrincipal user = (AdminPrincipal)session.getAttribute(AdminConstants.SESSION_USER_INFO);
        JSONObject object = JSONObject.parseObject(JSONObject.toJSONString(user));
        object.remove("token");
        object.remove("userId");
        return Result.success("ok", user);
    }
}
